package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GjhjjtjlbBsDetailBo;
import com.ruoyi.report_gc.domain.vo.GjhjjtjlbBsDetailVo;
import com.ruoyi.report_gc.domain.GjhjjtjlbBsDetail;
import com.ruoyi.report_gc.mapper.GjhjjtjlbBsDetailMapper;
import com.ruoyi.report_gc.service.IGjhjjtjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢筋焊接接头试验检测记录详情Service业务层处理
 *
 * @author llb
 * @date 2024-06-24
 */
@RequiredArgsConstructor
@Service
public class GjhjjtjlbBsDetailServiceImpl implements IGjhjjtjlbBsDetailService {

    private final GjhjjtjlbBsDetailMapper baseMapper;

    /**
     * 查询钢筋焊接接头试验检测记录详情
     */
    @Override
    public GjhjjtjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钢筋焊接接头试验检测记录详情列表
     */
    @Override
    public TableDataInfo<GjhjjtjlbBsDetailVo> queryPageList(GjhjjtjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GjhjjtjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<GjhjjtjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋焊接接头试验检测记录详情列表
     */
    @Override
    public List<GjhjjtjlbBsDetailVo> queryList(GjhjjtjlbBsDetailBo bo) {
        LambdaQueryWrapper<GjhjjtjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GjhjjtjlbBsDetail> buildQueryWrapper(GjhjjtjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GjhjjtjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, GjhjjtjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getHanjzl()), GjhjjtjlbBsDetail::getHanjzl, bo.getHanjzl());
        lqw.eq(StringUtils.isNotBlank(bo.getGangjph()), GjhjjtjlbBsDetail::getGangjph, bo.getGangjph());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), GjhjjtjlbBsDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getZhij() != null, GjhjjtjlbBsDetail::getZhij, bo.getZhij());
        lqw.eq(bo.getHanjcd() != null, GjhjjtjlbBsDetail::getHanjcd, bo.getHanjcd());
        lqw.eq(bo.getGangjjmj() != null, GjhjjtjlbBsDetail::getGangjjmj, bo.getGangjjmj());
        lqw.eq(bo.getJixhz() != null, GjhjjtjlbBsDetail::getJixhz, bo.getJixhz());
        lqw.eq(bo.getJixqd() != null, GjhjjtjlbBsDetail::getJixqd, bo.getJixqd());
        lqw.eq(StringUtils.isNotBlank(bo.getDuankxs()), GjhjjtjlbBsDetail::getDuankxs, bo.getDuankxs());
        lqw.eq(bo.getDuanklhkjl() != null, GjhjjtjlbBsDetail::getDuanklhkjl, bo.getDuanklhkjl());
        lqw.eq(bo.getYatzj() != null, GjhjjtjlbBsDetail::getYatzj, bo.getYatzj());
        lqw.eq(bo.getWanqjd() != null, GjhjjtjlbBsDetail::getWanqjd, bo.getWanqjd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjg()), GjhjjtjlbBsDetail::getShiyjg, bo.getShiyjg());
        lqw.eq(StringUtils.isNotBlank(bo.getHanjzlps()), GjhjjtjlbBsDetail::getHanjzlps, bo.getHanjzlps());
        return lqw;
    }

    /**
     * 新增钢筋焊接接头试验检测记录详情
     */
    @Override
    public Boolean insertByBo(GjhjjtjlbBsDetailBo bo) {
        GjhjjtjlbBsDetail add = BeanUtil.toBean(bo, GjhjjtjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钢筋焊接接头试验检测记录详情
     */
    @Override
    public Boolean updateByBo(GjhjjtjlbBsDetailBo bo) {
        GjhjjtjlbBsDetail update = BeanUtil.toBean(bo, GjhjjtjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GjhjjtjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋焊接接头试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
